#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include "proto.h"


int main(void)
{
    struct sockaddr_in readaddr;
    struct msg_st snd_data;
    ssize_t sent_bytes;
    int udp_socket = socket(AF_INET, SOCK_DGRAM, 0); // udp socket

    if (-1 == udp_socket) {
        perror("socket()");
        return 1;
    }

    // 发送数据
    snd_data.id = 102;
    strncpy(snd_data.name, "张三", NAMESIZE);

    readaddr.sin_family = AF_INET;
    inet_aton(RCVADDR, &readaddr.sin_addr); // 
    readaddr.sin_port = htons(PORT);

    // 发送数据
    sent_bytes = sendto(udp_socket, &snd_data,  sizeof(snd_data.id) + strlen(snd_data.name) + 1,\
            0, (struct sockaddr *)&readaddr, sizeof(readaddr));

    if (-1 == sent_bytes) {
        perror("sendto()");
        close(udp_socket);
        return 1;
    }

    printf("Sent %zd bytes to %s:%d\n", sent_bytes, RCVADDR, PORT);

	close(udp_socket);

    return 0;
}
